using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using AandEDLL.com.amazon.webservices;

namespace ItemSearchAPI
{
    class AmazonSearchService
    {
        enum categories
        {
            Apparel, Baby, Blended, Books, Classical, DVD, DigitalMusic,
            Electronics, GourmetFood, HealthPersonalCare, Jewelry, Kitchen, Magazines,
            Merchants, Miscellaneous, Music, MusicalInstruments, OfficeProducts,
            OutdoorLiving, PCHardware, PetSupplies, Photo, Restaurants, Software,
            SportingGoods, Tools, Toys, VHS, Video, VideoGames, Wireless, WirelessAccessories
        };

        public static List<ItemSearchResponse> getResults(String searchCritiera)
        {
            AWSECommerceService aws = new AWSECommerceService();
            ItemSearchRequest[] requests = new ItemSearchRequest[2];
            ItemSearchRequest request = null;
            ItemSearchResponse response = null;
            AandEDLL.com.amazon.webservices.ItemSearch itemSearch = new AandEDLL.com.amazon.webservices.ItemSearch();
            itemSearch.AWSAccessKeyId = "1R3YSHMW0J6WZJ65W9G2";
            List<ItemSearchResponse> responseList = new List<ItemSearchResponse>();

            for (int i = 0; i < Enum.GetNames(typeof(categories)).Length; i++)
            {
                request = new ItemSearchRequest();
                request.SearchIndex = Enum.GetName(typeof(categories), i);
                request.Keywords = searchCritiera;
                request.ResponseGroup = new string[] { "Small", "OfferSummary" };
                request.Sort = "salesrank";
                requests[i % 2] = request;

                if (i % 2 == 1)
                {
                    itemSearch.Request = requests;
                    try
                    {
                        response = aws.ItemSearch(itemSearch);
                    }
                    catch
                    {
                        //do nothing
                    }

                    if (response != null)
                        responseList.Add(response);
                }
            }

            return responseList;
        }
    }
}
